﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.MiniBox.Entity;
using Pub.Class;

namespace GTS.MiniBox.Helper {
    /// <summary>
    /// 操作类
    /// 
    /// 修改纪录
    ///     2012-07-31 版本：1.0 系统自动创建此类
    /// 
    /// </summary>
    public partial class ChannelDealDataHelper {
        /// <summary>
        /// 缓存多少秒 x 5
        /// </summary>
        public static int cacheSeconds = 1440;
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="channelDealData">实体类</param>
        /// <param name="delCache">添加成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>添加是否成功</returns>
        public static bool Insert(ChannelDealData channelDealData, string dbkey = "", string[] delCache = null) {
            string strSql = new SQL().Insert(ChannelDealData._)
                .Value(ChannelDealData._SPCode, channelDealData.SPCode)
                .Value(ChannelDealData._LinkID, channelDealData.LinkID)
                .Value(ChannelDealData._SMSKey, channelDealData.SMSKey)
                .Value(ChannelDealData._LongPhone, channelDealData.LongPhone)
                .Value(ChannelDealData._Mobile, channelDealData.Mobile)
                .Value(ChannelDealData._ReceiveQuery, channelDealData.ReceiveQuery)
                .Value(ChannelDealData._ResponseStatus, channelDealData.ResponseStatus)
                .Value(ChannelDealData._RequestDate, channelDealData.RequestDate)
                .Value(ChannelDealData._MountCode, channelDealData.MountCode)
                .Value(ChannelDealData._GameCode, channelDealData.GameCode)
                .Value(ChannelDealData._ChargeCode, channelDealData.ChargeCode)
                .Value(ChannelDealData._Price, channelDealData.Price)
                .Value(ChannelDealData._IsHidden, channelDealData.IsHidden)
                .Value(ChannelDealData._DeviceCode, channelDealData.DeviceCode)
                .Value(ChannelDealData._MRDate, channelDealData.MRDate)
                .Value(ChannelDealData._MODate, channelDealData.MODate)
                .Value(ChannelDealData._Telcoid, channelDealData.Telcoid)
                .Value(ChannelDealData._CountryCode, channelDealData.CountryCode)
                .Value(ChannelDealData._Unit, channelDealData.Unit)
                .Value(ChannelDealData._Area, channelDealData.Area)
                .Value(ChannelDealData._PLMN, channelDealData.PLMN)
                .Value(ChannelDealData._SMSContent, channelDealData.SMSContent)
                .Value(ChannelDealData._IMSI, channelDealData.IMSI)
                .Value(ChannelDealData._CityCode, channelDealData.CityCode)
                .Value(ChannelDealData._ChannelID, channelDealData.ChannelID)
                .ToString();
            bool istrue = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) == 1 : Data.Pool(dbkey).ExecSql(strSql) == 1;
            if (delCache.IsNull()) return istrue;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.ChannelDealDataCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.ChannelDealDataCache_" + s + ")" : s);
            }
            return istrue;
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="channelDealData">实体类</param>
        /// <returns>添加是否成功</returns>
        public static bool Insert(ChannelDealData channelDealData, string dbkey = "") {
            return Insert(channelDealData, dbkey, null);
        }
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="channelDealData">实体类</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(ChannelDealData channelDealData, string dbkey = "", string where = "", string[] delCache = null) {
            if (channelDealData.SPCode.IsNullEmpty()  && channelDealData.LinkID.IsNullEmpty()) return false;
            string strSql = new SQL().Update(ChannelDealData._)
                .Set(ChannelDealData._SMSKey, channelDealData.SMSKey)
                .Set(ChannelDealData._LongPhone, channelDealData.LongPhone)
                .Set(ChannelDealData._Mobile, channelDealData.Mobile)
                .Set(ChannelDealData._ReceiveQuery, channelDealData.ReceiveQuery)
                .Set(ChannelDealData._ResponseStatus, channelDealData.ResponseStatus)
                .Set(ChannelDealData._RequestDate, channelDealData.RequestDate)
                .Set(ChannelDealData._MountCode, channelDealData.MountCode)
                .Set(ChannelDealData._GameCode, channelDealData.GameCode)
                .Set(ChannelDealData._ChargeCode, channelDealData.ChargeCode)
                .Set(ChannelDealData._Price, channelDealData.Price)
                .Set(ChannelDealData._IsHidden, channelDealData.IsHidden)
                .Set(ChannelDealData._DeviceCode, channelDealData.DeviceCode)
                .Set(ChannelDealData._MRDate, channelDealData.MRDate)
                .Set(ChannelDealData._MODate, channelDealData.MODate)
                .Set(ChannelDealData._Telcoid, channelDealData.Telcoid)
                .Set(ChannelDealData._CountryCode, channelDealData.CountryCode)
                .Set(ChannelDealData._Unit, channelDealData.Unit)
                .Set(ChannelDealData._Area, channelDealData.Area)
                .Set(ChannelDealData._PLMN, channelDealData.PLMN)
                .Set(ChannelDealData._SMSContent, channelDealData.SMSContent)
                .Set(ChannelDealData._IMSI, channelDealData.IMSI)
                .Set(ChannelDealData._CityCode, channelDealData.CityCode)
                .Set(ChannelDealData._ChannelID, channelDealData.ChannelID)
                .Where("1=1").Where(new Where()
                    .And(ChannelDealData._SPCode, channelDealData.SPCode, Operator.Equal, true)
                    .And(ChannelDealData._LinkID, channelDealData.LinkID, Operator.Equal, true)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) : Data.Pool(dbkey).ExecSql(strSql);
            if (value <= 0) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.ChannelDealDataCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.ChannelDealDataCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="channelDealData">实体类</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(ChannelDealData channelDealData, string dbkey = "") {
            return Update(channelDealData, dbkey, "", null);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="sPCode">SP英文名称</param>
        /// <param name="linkID">单号</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">删除成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(string sPCode, string linkID,  string dbkey = "", string where = "", string[] delCache = null) {
            if (sPCode.IsNullEmpty()  && linkID.IsNullEmpty()) return false;
            string strSql = new SQL().Delete(ChannelDealData._)
                .Where("1=1").Where(new Where()
                    .And(ChannelDealData._SPCode, sPCode, Operator.Equal, true)
                    .And(ChannelDealData._LinkID, linkID, Operator.Equal, true)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) :  Data.Pool(dbkey).ExecSql(strSql);
            if (value != 1) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.ChannelDealDataCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.ChannelDealDataCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="sPCode">SP英文名称</param>
        /// <param name="linkID">单号</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(string sPCode, string linkID, string dbkey = "") {
            return DeleteByID(sPCode, linkID,  dbkey, "", null);
        }
        /// <summary>
        /// 按主键查询，返回数据的实体类
        /// </summary>
        /// <param name="sPCode">SP英文名称</param>
        /// <param name="linkID">单号</param>
        /// <param name="where">附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时随机取连接key</param>
        /// <returns>返回单条记录的实体类</returns>
        public static ChannelDealData SelectByID(string sPCode, string linkID,  string dbkey = "", string where = "") {
            string cacheNameKey = "GTS.MiniBox.ChannelDealDataCache_SelectByID_{0}".FormatWith(sPCode + "_" + linkID + "_" +  "_" + where);
            return Cache2.Get<ChannelDealData>(cacheNameKey, cacheSeconds, () => {
                string strSql = new SQL().From(ChannelDealData._)
                    .Select(ChannelDealData._SPCode)
                    .Select(ChannelDealData._LinkID)
                    .Select(ChannelDealData._SMSKey)
                    .Select(ChannelDealData._LongPhone)
                    .Select(ChannelDealData._Mobile)
                    .Select(ChannelDealData._ReceiveQuery)
                    .Select(ChannelDealData._ResponseStatus)
                    .Select(ChannelDealData._RequestDate)
                    .Select(ChannelDealData._MountCode)
                    .Select(ChannelDealData._GameCode)
                    .Select(ChannelDealData._ChargeCode)
                    .Select(ChannelDealData._Price)
                    .Select(ChannelDealData._IsHidden)
                    .Select(ChannelDealData._DeviceCode)
                    .Select(ChannelDealData._MRDate)
                    .Select(ChannelDealData._MODate)
                    .Select(ChannelDealData._Telcoid)
                    .Select(ChannelDealData._CountryCode)
                    .Select(ChannelDealData._Unit)
                    .Select(ChannelDealData._Area)
                    .Select(ChannelDealData._PLMN)
                    .Select(ChannelDealData._SMSContent)
                    .Select(ChannelDealData._IMSI)
                    .Select(ChannelDealData._CityCode)
                    .Select(ChannelDealData._ChannelID)
                    .Where("1=1").Where(new Where()
                        .And(ChannelDealData._SPCode, sPCode, Operator.Equal)
                        .And(ChannelDealData._LinkID, linkID, Operator.Equal)
                    ).ToString();
                if (!where.IsNullEmpty()) strSql += where;
                return Data.Pool(dbkey).GetDbDataReader(strSql).ToList<ChannelDealData>().FirstOrDefault();
            });
        }
        /// <summary>
        /// 按主键查询，返回数据的实体类
        /// </summary>
        /// <param name="sPCode">SP英文名称</param>
        /// <param name="linkID">单号</param>
        /// <returns>返回单条记录的实体类</returns>
        public static ChannelDealData SelectByID(string sPCode, string linkID, string dbkey = "") {
            return SelectByID(sPCode, linkID,  dbkey, "");
        }
        /// <summary>
        /// 清除按主键查询的缓存
        /// </summary>
        public static void ClearCacheSelectByID(string sPCode, string linkID,  string where = "") {
            string cacheName = "GTS.MiniBox.ChannelDealDataCache_SelectByID_{0}";
            string cacheNameKey = string.Format(cacheName, sPCode + "_" + linkID + "_" +  "_" + where);
            Cache2.Remove(cacheNameKey);
        }
        /// <summary>
        /// 查询数据，带分页
        /// </summary>
        /// <param name="pageIndex">当前第几页，从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="totalRecords">返回总记录数</param>
        /// <param name="where">附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="order">排序字段，不加“order by”</param>
        /// <param name="fieldList">设置需要返回的字段</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时随机取连接key</param>
        /// <param name="pageEnum">使用哪种分页方式（not_in、max_top、top_top、row_number、mysql、oracle、sqlite）</param>
        /// <returns>返回实体记录集</returns>
        public static IList<ChannelDealData> SelectPageList(int pageIndex, int pageSize, out int totalRecords, string dbkey = "", string where = "", string order = "", string fieldList = "", PagerSQLEnum pageEnum = PagerSQLEnum.row_number) {
            totalRecords = 0;
            string cacheNameKey = "GTS.MiniBox.ChannelDealDataCache_SelectPageList_{0}_{1}_{2}_{3}_{4}".FormatWith(pageIndex, pageSize, where, order, fieldList);
            string cacheRecordsKey = "GTS.MiniBox.ChannelDealDataCache_RecordsSelectPageList_{0}_{1}_{2}_{3}_{4}".FormatWith(pageIndex, pageSize, where, order, fieldList);
            IList<ChannelDealData> list = (IList<ChannelDealData>)Cache2.Get(cacheNameKey);
            if (!list.IsNull()) { totalRecords = (int)Cache2.Get(cacheRecordsKey); return list; }

            using (PagerSQLHelper s = new PagerSQLHelper(pageEnum)) {
                PagerSql sql = s.GetSQL(pageIndex, pageSize, ChannelDealData._, ChannelDealData._SPCode, fieldList.IfNullOrEmpty("[SPCode],[LinkID],[SMSKey],[LongPhone],[Mobile],[ReceiveQuery],[ResponseStatus],[RequestDate],[MountCode],[GameCode],[ChargeCode],[Price],[IsHidden],[DeviceCode],[MRDate],[MODate],[Telcoid],[CountryCode],[Unit],[Area],[PLMN],[SMSContent],[IMSI],[CityCode],[ChannelID],"), where, "", order);
                DataSet ds = Data.Pool(dbkey).GetDataSet(sql.DataSql + ";" + sql.CountSql);
                if (ds.IsNull()) return list;
                list = ds.Tables[0].ToList<ChannelDealData>();
                totalRecords = ds.Tables[1].Rows[0][0].ToInt();
                ds.Dispose(); ds = null;
            }
            Cache2.Insert(cacheNameKey, list, cacheSeconds);
            Cache2.Insert(cacheRecordsKey, totalRecords, cacheSeconds);
            return list;
        }
        /// <summary>
        /// 查询记录，带分页
        /// </summary>
        /// <param name="pageIndex">当前第几页，从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="totalRecords">返回总记录数</param>
        /// <returns>返回实体记录集</returns>
        public static IList<ChannelDealData> SelectPageList(int pageIndex, int pageSize, out int totalRecords, string dbkey = "") {
            return SelectPageList(pageIndex, pageSize, out totalRecords, dbkey, "", "", "", PagerSQLEnum.row_number);
        }
        /// <summary>
        /// 清除查询记录，带分页的缓存
        /// </summary>
        public static void ClearCacheSelectPageList() {
            string cacheNameKey = "GTS.MiniBox.ChannelDealDataCache_SelectPageList_(.+?)";
            string cacheRecordsKey = "GTS.MiniBox.ChannelDealDataCache_RecordsSelectPageList_(.+?)";
            Cache2.RemoveByPattern(cacheNameKey);
            Cache2.RemoveByPattern(cacheRecordsKey);
        }
    }
}

